import {Directive, EventEmitter, Output} from '@angular/core';
import {CookieService} from "ngx-cookie-service";
import {HttpService} from "../service/http.service";

@Directive({
    selector: '[appAuth]',
    providers: [
        CookieService,
        HttpService
    ]
})
export class AuthDirective {
    @Output() checkAuth: EventEmitter<any> = new EventEmitter();

    constructor(public CookieService: CookieService, public http: HttpService) {
       
    }

    ngOnInit() {

        let userid = this.CookieService.get('userid');
        if (!!userid) {
            this.http.auth(userid).subscribe((res:any) => {
                if(!!res.result){
                    this.checkAuth.emit(res.result.doc);

                }else{
                    this.checkAuth.emit(null);
                }

            });
        } else {
            this.checkAuth.emit(null);
        }
    }



}
